package com.jprocms.module.cms.controller.portal.api;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jpro.framework.tenant.core.context.TenantContextHolder;
import com.jpro.module.system.api.permission.PermissionApi;
import com.jprocms.module.cms.constant.CmsSysConstants;
import com.jprocms.module.cms.controller.admin.channel.vo.ChannelFrontQueryReqVO;
import com.jprocms.module.cms.controller.admin.channel.vo.ChannelListReqVO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.enums.EnumIntCommonStatus;
import com.jprocms.module.cms.service.channel.ChannelService;
import com.jprocms.module.cms.service.content.ContentCountService;
import com.jprocms.module.cms.service.site.SiteService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.jprocms.module.cms.constant.CmsSysConstants.FRONT_API;

/**
 * 栏目前台 接口
 *
 * @author jprocms
 */
@Slf4j
@Tag(name = "前台 - 栏目接口")
@RestController
@RequestMapping({FRONT_API + "/channel"})
public class ChannelApiController {
    private final SiteService siteResolver;
    private final ChannelService channelService;

    private final ContentCountService contentCountService;

    private final PermissionApi permissionApi;

    public ChannelApiController(SiteService siteResolver, ChannelService channelService,
                                ContentCountService contentCountService, PermissionApi permissionApi) {
        this.siteResolver = siteResolver;
        this.channelService = channelService;
        this.contentCountService = contentCountService;
        this.permissionApi = permissionApi;
    }

    @Operation(summary = "获取栏目列表")
    @GetMapping(value = "/list")
    public CommonResult<List<ChannelDO>> list(@ModelAttribute ChannelFrontQueryReqVO queryReqVO, HttpServletRequest request) {
        SiteDO site = CmsContextUtil.getSite();
        if (queryReqVO.getSiteId() == null) {
            queryReqVO.setSiteId(site.getId());
        }
        //设置了parentPath 以 parentPath为准
        if (StringUtils.hasText(queryReqVO.getParentPath())) {
            ChannelDO channelDO = channelService.selectByChannelPath(queryReqVO.getParentPath(), queryReqVO.getSiteId());
            if (channelDO != null) {
                queryReqVO.setParentId(channelDO.getId());
            }
        }
        ChannelListReqVO listReqVO = ChannelListReqVO.of(queryReqVO.getSiteId(), queryReqVO.getParentId(), queryReqVO.getIncludeChild(), EnumIntCommonStatus.OPEN.getStatus());
        return CommonResult.success(channelService.getChannelListForFront(listReqVO));
    }

    @Operation(summary = "获取栏目详情")
    @ApiResponses(value = {@ApiResponse(description = "栏目详情")})
    @GetMapping("/{id:[\\d]+}")
    public CommonResult<ChannelDO> get(@Parameter(description = "栏目ID") @PathVariable Long id) {
        return CommonResult.success(channelService.getChannel(id));
    }

    @Operation(summary = "获取栏目详情")
    @ApiResponses(value = {@ApiResponse(description = "栏目详情")})
    @GetMapping("/getByChannelPath/{channelPath}")
    public CommonResult<ChannelDO> getByChannelPath(@Parameter(description = "栏目别名") @PathVariable String channelPath,
                                                    @Parameter(description = "站点ID") Long siteId,
                                                    HttpServletRequest request) {
        if (siteId == null) {
            siteId = CmsContextUtil.getCurrSiteId();
        }
        return CommonResult.success(channelService.selectByChannelPath(channelPath, siteId));
    }

    @Operation(summary = "获取栏目浏览次数")
    @GetMapping("/view/{id:[\\d]+}")
    public CommonResult<Long> view(@Parameter(description = "栏目ID") @PathVariable Long id) {
        return CommonResult.success(contentCountService.viewChannel(id));
    }

    @Operation(summary = "检查是否可以查看栏目")
    @ApiResponses(value = {@ApiResponse(description = "栏目详情")})
    @GetMapping("/hasChannelPermission")
    public CommonResult<Boolean> hasChannelPermission(@Parameter(description = "栏目ID", name = "channelId") Long channelId) {
        TenantContextHolder.setTenantId(CmsSysConstants.CMS_TENANT_ID);
        return permissionApi.hasChannelPermission(SecurityFrameworkUtils.getLoginUserId(), channelId);
    }

}
